#include <my_header.h>
#include "InternetOperator.h"
#include "Analysis.h"
#include "Stack.h"

typedef struct {
    int length;
    char data[1024];
}Train;

int main()
{
    char * ip = "192.168.220.128";
    char * port = "8888";
    int sockfd = socket(AF_INET,SOCK_STREAM,0);
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(atoi(port));
    addr.sin_addr.s_addr = inet_addr(ip);
    connect(sockfd,(struct sockaddr *)&addr,sizeof(addr));
    int epollfd = epoll_create(1);
    epollAdd(epollfd,sockfd);
    struct epoll_event ready_set[5];
    Train train;
    while(1){
        char username[1024] = {0};
        char password[1024] = {0};
        printf("请输入用户名：");
        fflush(stdout);
        scanf("%s",username);
        if(username[strlen(username)-1] == '\n'){
            username[strlen(username)-1] = '\0';
        }
        printf("\n");
        printf("请输入密码：");
        fflush(stdout);
        scanf("%s",password);
        if(password[strlen(password)-1] == '\n'){
            password[strlen(password)-1] = '\0';
        }
        printf("\n");

        train.length = strlen(username);
        memcpy(train.data,username,train.length);
        send(sockfd,&train,sizeof(train.length)+train.length,MSG_NOSIGNAL);

        train.length = strlen(password);
        memcpy(train.data,password,train.length);
        send(sockfd,&train,sizeof(train.length)+train.length,MSG_NOSIGNAL);

        epoll_wait(epollfd,ready_set,5,-1);
        char c;
        recv(ready_set[0].data.fd,&c,1,MSG_WAITALL);
        if(c == '1'){
            printf("登录成功!\n");
            break;
        }else{
            printf("用户名或密码错误！请重新登录!\n");
        }

    }
    Stack * s = (Stack *)calloc(1,sizeof(Stack));   //路径栈
    stackInit(s);
    char * bottom = (char *)calloc(2,sizeof(char));
    bottom[0] = '/';     
    push(s,bottom);

    char buf[4096] = {0};

    int flag = 0;      //标记客户端或服务端主动退出

    epollAdd(epollfd,STDIN_FILENO);

    while(1){
        char s_curpath[256] = {0};      
        if(s->size == 1){      
            s_curpath[0] = '/';
        }else{
            for(int i = 1 ; i < s->size ;++i){
                char xiegang[] = "/";
                char ass_p[64] = {0};
                memcpy(ass_p,s->str[i],strlen(s->str[i]));
                strcat(s_curpath,xiegang);
                strcat(s_curpath,ass_p);
            }
        }
        s_curpath[strlen(s_curpath)] = '$';
        printf("%s",s_curpath);
        fflush(stdout);
        int epoll_ret = epoll_wait(epollfd,ready_set,5,-1);     
        for(int i = 0 ; i < epoll_ret ; ++i){
            if(ready_set[i].data.fd == STDIN_FILENO){
                bzero(buf,sizeof(buf));
                int rret = read(STDIN_FILENO,buf,sizeof(buf));
                if(buf[0] == '\n'){
                    break;
                }
                if(rret == 0){
                    flag = 1;   
                    break;
                }
                int pose_ret;
                char * p ;
                switch (analysis(buf,sockfd)) {   // 不同命令返回值不同，根据返回值判断时候向后台传参
                case 0:
                    pose_ret = cdDispose(buf,sockfd,s);  //一、更新并检查路径对不对。（防止越界）二、向后台发送路径看有没有 三、更新路径栈
                    printf("cd返回值%d\n",pose_ret);
                    break;
                case 1:
                    lsDispose(s,sockfd);
                    break;
                case 2:
                    pwdDispose(s);
                    break;
                case 3:
                    p = checkPath(buf,s);   //得到干净的服务器路径（当然只是一部分）
                    mkdirDispose(p,sockfd);
                    break;
                case 4:
                    p = checkPath(buf,s);
                    rmdirDispose(p,sockfd);
                    break;
                case 5:
                    p = checkPath(buf,s);
                    rmfileDispose(p,sockfd);
                    break;
                case 6:
                    pose_ret = putsDispose(buf,sockfd,s_curpath);    // 输入信息、套接字、服务器当前工作目录
                    printf("puts返回值%d\n",pose_ret);
                    break;
                case 7:
                    p = checkPath(buf,s);   //得到干净的服务器路径
                    if(p == NULL){
                        printf("越界错误!\n");
                    }else{
                        pose_ret =  getsDispose(p,sockfd);    
                        printf("gets返回值%d\n",pose_ret);
                    }
                    break;
                default:
                    printf("未知命令\n");
                    break;
                }
            }else if(ready_set[i].data.fd == sockfd){
                bzero(buf,sizeof(buf));
                int rret = recv(sockfd,buf,sizeof(buf),MSG_WAITALL);
                if(rret == 0){
                    flag = 1;
                    break;
                }
            }
        }
        if(flag == 1){
            break;
        }
    }
    return 0;
}

